Declaring Internal Entities

Now that you know how to create internal entities, where should you declare them.  As of yet, we have not yet discussed document type definitions DTD’s in any detail.  This will be your first entrée into the world of DTD’s.

To declare an internal entity, you must first declare a document type definition.  This is done using the <!DOCTYPE> tag.  The syntax is:

<!DOCTYPE name external.DTD [ internal.declarations ]>

Do not worry about the “external.DTD” reference for now.  Instead, we need to focus on the internal declarations section.  This is where we declare our internal entities.  Using our previous example, let’s look at the entire XML document and the entity declarations it contains.

<?xml version="1.0"?>
<!DOCTYPE entity.examples [
	<!ENTITY example1 "John Doe">
	<!ENTITY example2 "John &#38;#38; Jane">
	<!ENTITY example3 "John &amp; Jane again">
]>
<entity.examples>
	<element1>&example1;</element1>
	<element2>&example2;</element2>
	<element3>&example3;</element3>
</entity.examples>

File: Chapter 3\InternalEntities.xml

Let’s look at this document using Internet Explorer 5.0.

File: Chapter 3\InternalEntities.xml in IE5

Let’s look a little closer at our DTD definition.

<!DOCTYPE entity.examples [
	<!ENTITY example1 "John Doe">
	<!ENTITY example2 "John &#38;#38; Jane">
	<!ENTITY example3 "John &amp; Jane again">
]>
<entity.examples>
. . .
</entity.examples>

The first point that we want to make is the <!DOCTYPE keyword and the ]> termination.  Do not inject spaces or tabs anywhere in these character strings.  It will result in misinterpretation of your XML markup (i.e. you will have fatal errors).

The second point is that the name of the DTD (entity.examples) is the same as the root element.  Get in the habit of doing this as it is a requirement for well-formed, valid documents.

Third, all entity declarations occur between the open bracket ( [ ) and the close bracket ( ] ).  These brackets are beginning and ending points for the document definition declarations, which include internal entities.

This is enough coverage of DTD’s for now.  We will get into content modeling with DTD’s later in this course.